﻿using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Library.WebControls.SimpleCheckList {
	[DesignTimeVisible(false)]
	[ToolboxItem(false)]
	public class SpCheckListItemTemplate : HtmlGenericControl, IDataItemContainer {
		//internal bool IsAll { get; set; }
		internal SpCheckList CheckList { get; set; }
		private HtmlGenericControl _divCheckbox;
		private CheckBox _checkBox;
		private HtmlGenericControl _divLabel;
		protected const string MenuItemCss = "chlMenuItem";
		protected const string ActiveMenuItemCss = "chlMenuItemActive";
		protected const string SelectMenuItemCss = "chlMenuItemSelect";

		public SpCheckListItemTemplate()
			: base("div") {
			Attributes["class"] = MenuItemCss;
		}

		protected override void CreateChildControls() {
			_divCheckbox = new HtmlGenericControl("span");
			_checkBox = new CheckBox();
			_divCheckbox.Controls.Add(_checkBox);
			_divCheckbox.Attributes["class"] = "chlItemCheck";
			_divLabel = new HtmlGenericControl("span");
			_divLabel.Attributes["class"] = "chlItemText";
			Controls.Add(_divCheckbox);
			Controls.Add(_divLabel);
		}
		public bool Checked {
			get {
				return Attributes["class"] == SelectMenuItemCss;
			}
			set {
				EnsureChildControls();
				_checkBox.Checked = value;
				Attributes.Remove("class");
				if (value) {
					Attributes["class"] = SelectMenuItemCss;
				} else {
					Attributes["class"] = MenuItemCss;
				}
			}
		}

		public object DataItem { get; internal set; }

		public int DataItemIndex { get; internal set; }

		public int DisplayIndex { get; internal set; }
		protected override void Render(HtmlTextWriter writer) {
		//	i/f (IsAll)
			//	Attributes["onclick"] = CheckList.ClientName + ".ChangeAll(this);";
		//	else
				Attributes["onclick"] = CheckList.ClientName + ".Call(3,this);";
			_checkBox.InputAttributes["onclick"] =  CheckList.ClientName+ ".Call(3,this);";
			base.Render(writer);
		}
		public string Value {
			get {
				return Attributes["_spexcomboboxkey_"];
			}
			set { Attributes["_spexcomboboxkey_"] = value; }
		}

		internal HtmlGenericControl TextControl {
			get {
				EnsureChildControls();
				return _divLabel;
			}
		}

		public string Text {
			get {
				EnsureChildControls();
				return _divLabel.InnerText;
			}
			set {
				EnsureChildControls();
				_divLabel.InnerText = value;
				Attributes["_spexcomboboxvalue_"] = value;
			}

		}
	}
}
